<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        style="width: 200px;"
        class="filter-item"
        placeholder="订单编号"
        v-model="queryForm.voucherCode"
      ></el-input>
      <el-input
        style="width: 200px;"
        class="filter-item"
        placeholder="设备编号"
        v-model="queryForm.deviceCode"
      ></el-input>
      <el-select
        clearable
        style="width: 170px"
        class="filter-item"
        v-model="queryForm.step"
        placeholder="投放结果"
      >
        <el-option
          v-for="(val, key) in Dicts.AD_PUTIN_ORDER_DETAIL_STEP"
          :key="key"
          :label="val"
          :value="key"
        ></el-option>
      </el-select>
      <el-select
        clearable
        style="width: 170px"
        class="filter-item"
        v-model="queryForm.regionRange"
        placeholder="投放范围"
      >
        <el-option
          v-for="(val, key) in Dicts.AD_RANGE"
          :key="key"
          :label="val"
          :value="key"
        ></el-option>
      </el-select>
      <el-button
        class="filter-item"
        @click="doQuery"
        type="primary"
        icon="el-icon-search"
        >查询</el-button
      >
      <el-button
        v-permission="'putinOrderDetail:lunchs'"
        class="filter-item"
        @click="resendInstruction"
        type="success"
        icon="el-icon-message"
        >重新批量投放</el-button
      >
    </div>
    <el-table
      v-loading="loading"
      :data="records"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="100" align="center">
      </el-table-column>
      <el-table-column prop="rowId" label="ID" width="100" align="center">
      </el-table-column>
      <el-table-column
        prop="voucherCode"
        label="订单编号"
        width="250"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="regionRange"
        label="投放范围"
        width="200"
        align="center"
      >
        <template slot-scope="scope">
          {{ Dicts.AD_RANGE[scope.row.regionRange] }}
        </template>
      </el-table-column>
      <el-table-column prop="type" label="投放类型" width="200" align="center">
        <template slot-scope="scope">
          {{ Dicts.AD_PUTIN_ORDER_TYPE[scope.row.type] }}
        </template>
      </el-table-column>
      <el-table-column prop="step" label="投放结果" width="200" align="center">
        <template slot-scope="scope">
          {{ Dicts.AD_PUTIN_ORDER_DETAIL_STEP[scope.row.step] }}
        </template>
      </el-table-column>
      <el-table-column
        prop="deviceCode"
        label="设备编号"
        width="250"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="playTimes"
        label="播放次数"
        width="200"
        align="center"
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="操作"
        class-name="small-padding"
        fixed="right"
        width="200"
      >
        <template slot-scope="scope">
          <el-button
            v-permission="'putinOrderDetail:lunch'"
            type="primary"
            size="mini"
            v-if="
              scope.row.step !== '6' &&
              scope.row.step !== '10' &&
              scope.row.step !== '12'
                ? true
                : false
            "
            @click="notifyById(scope.row.rowId)"
            title="重新投放"
            icon="el-icon-sort"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryForm.current"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="queryForm.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="queryForm.total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { queryPutinOrderLog, notifyById, notifyByIds } from '@/api/ad'
export default {
  name: 'putinOrderLog',
  data() {
    return {
      multipleSelection: [],
      loading: false,
      queryForm: {
        current: 1,
        pages: 0,
        total: 0,
        size: 10,
        title: undefined
      },
      dates: [],
      dateTime: '',
      adBidMark: [],
      records: [],
      rowId: []
    }
  },
  created() {
    this.initOssClient().then(_ => {
      this.doQuery()
    })
  },
  methods: {
    upload(options) {
      this.uploadForElement(options)
    },
    uploadAd(response, file, fileList) {
      this.editForm.mediaUrl = response.name
    },
    handleCurrentChange(current) {
      this.queryForm.current = current
      this.doQuery()
    },
    handleSizeChange(size) {
      this.queryForm.size = size
      this.doQuery()
    },
    doQuery() {
      this.loading = true
      queryPutinOrderLog(this.queryForm).then(({ data: { data } }) => {
        this.queryForm.current = data.current
        this.queryForm.pages = data.pages
        this.queryForm.total = data.total
        this.queryForm.size = data.size
        this.records = data.records
        this.loading = false
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    resendInstruction() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          showClose: true,
          message: '请选择要重新投放的订单',
          type: 'error'
        })
      } else {
        this.orderId = []
        this.orderDetailId = []
        this.deviceCode = []
        for (let index = 0; index < this.multipleSelection.length; index++) {
          if (
            this.multipleSelection[index].step !== '6' &&
            this.multipleSelection[index].step !== '10' &&
            this.multipleSelection[index].step !== '12'
          ) {
            this.rowId.push(this.multipleSelection[index].rowId)
          }
        }
        notifyByIds(this.rowId).then(({ data: { data, msg, code } }) => {
          if (code === 0) {
            this.doQuery()
          }
          this.$message({
            showClose: true,
            message: '通知已发送',
            type: 'success'
          })
        })
      }
    },
    notifyById(rowId) {
      this.$confirm('此操作将重新投放广告, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        notifyById(rowId).then(({ data: { data, msg } }) => {
          this.doQuery()
          this.$message({
            showClose: true,
            message: '通知已发送',
            type: 'success'
          })
        })
      })
    }
  },
  components: {}
}
</script>

<style scoped></style>
